package com.xlh.dto.courseTest.practice;

import com.xlh.dto.courseTest.AnswerSheetTypeDTO;
import com.xlh.enums.courseTest.QuestionTypeEnum;
import com.xlh.pojo.courseTest.CourseQuestionRepository;
import com.xlh.pojo.courseTest.CourseQuestionRepositoryDetails;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr 2019/4/10
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
public class NotSubmitDTO extends PracticeDTO {
    /**
     * 题干信息
     */
    // 题目id
    private Long id;
    // 题号
    private Integer orderNumber;
    // 题目数量
    private Integer questionCount;
    // 题型
    private Integer type;
    // 题目分数
    private Integer score;
    // 题型名称
    private String typeName;
    // 题干
    private String content;
    // 选项信息
    private List<String> choiceList;
    // 是否为多选题
    private Boolean isMultipleChoice;

    /**
     * 考试剩余时间
     */
    private Long timeLeft;

    /**
     * 答题卡信息
     */
    // 全局答题卡列表
    List<AnswerSheetTypeDTO> answerSheetList;
    // 答题卡id
    private Long answerSheetId;
    // 学生当前题目填写的答案
    private Object answer;
    // 正确答案
    private Object rightAnswer;
    //解析
    private String jxContent;
    public NotSubmitDTO() {
        setSubmit(false);
    }

    public <T> void convert(
            CourseQuestionRepository question,
            int orderNumber,
            T answer,
            int questionCount,
            List<CourseQuestionRepositoryDetails> choiceList
    ) {
        // 设置题干的基本内容
        this.id = question.getId();
        this.orderNumber = orderNumber;
        this.answer = answer;
        this.type = question.getQuestionType();
        this.content = question.getHtmlContent();
        this.questionCount = questionCount;
        this.jxContent=jxContent;


        QuestionTypeEnum questionTypeEnum = QuestionTypeEnum.forCode(question.getQuestionType());
        if (questionTypeEnum != null) {
            this.typeName = questionTypeEnum.getMessage();
        }

        if (CollectionUtils.isNotEmpty(choiceList)) {
            this.choiceList = choiceList.stream().map(CourseQuestionRepositoryDetails::getContent).collect(Collectors.toList());
            this.isMultipleChoice = choiceList.stream().filter(CourseQuestionRepositoryDetails::getIsCorrect).count() > 1;
        }
    }
}
